package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.wx.cart.*;
import com.cskaoyan.service.wx.WxCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname CartController
 * @Description
 * @Date 2021/8/14 14:39
 * @Created by songweinan
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService cartService;

    @GetMapping("index")
    public BaseRespVo index(){

        IndexVo indexVo = cartService.getCartIndex();
        return BaseRespVo.ok(indexVo);
    }

    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CheckedBo checkedBo){

        IndexVo indexVo = cartService.getCartIndexChanged(checkedBo);
        return BaseRespVo.ok(indexVo);
    }

    @PostMapping("add")
    public BaseRespVo add(@RequestBody AddCartBo addCartBo){

        //把这件商品加入购物车后当前购物车的总件数
        Integer cartTotal = cartService.addCart(addCartBo);

        return BaseRespVo.ok(cartTotal);
    }

    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody AddCartBo addCartBo){

        //返回这条数据新加入购物车的cartId
        Integer cartId = cartService.fastadd(addCartBo);

        return BaseRespVo.ok(cartId);
    }

    @GetMapping("goodscount")
    public BaseRespVo goodsCount(){

        Integer cartTotal = cartService.getGoodsCount();
        if(cartTotal == null){
            return BaseRespVo.ok(0);
        }
        return BaseRespVo.ok(cartTotal);
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody UpdateCartBo updateCartBo){

        Integer updateCode = cartService.updateCart(updateCartBo);

        return BaseRespVo.ok();
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody DeleteCartsBo deleteCartsBo){

        IndexVo indexVo = cartService.deleteCarts(deleteCartsBo);
        return BaseRespVo.ok(indexVo);
    }

    /**
     * 生成订单信息
     * @param cartId 0
     * @param addressId
     * @param couponId
     * @param grouponRulesId 0
     * @return
     */
    @GetMapping("checkout")
    public BaseRespVo checkout(Integer cartId,Integer addressId,Integer couponId,Integer grouponRulesId){

        CheckOutVo checkOutVo = cartService.addOrder(cartId,addressId,couponId,grouponRulesId);

        return BaseRespVo.ok(checkOutVo);
    }
}
